<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SCDB
 *
 * @author vlad
 */
class SCDB {
    //put your code here
    
    private $_config = array();
    private $_pid = NULL;
    private $_dbh = NULL;
    private $_isConnected = false;
    private $_isPersistent = false;
    private $_selectedDatabase = NULL;
    
    public function __construct($inConfig) {
        //print "Creating SCDB instance\n";
        if (is_array($inConfig)) {
            $this->_config = $inConfig;
        }
    }
    
    public function connect($asPersistent = false) {
        //print "SCDB::connect()\n";
        if (
                   !empty($this->_config["DBNAME"])
                && !empty($this->_config["DBUSER"])
                && !empty($this->_config["DBHOST"])
                && !defined($this->_config["DBPASS"])
               
           ) {
            
            if ($asPersistent) {
                $this->_dbh = mysql_pconnect();
            } else {
                $this->_dbh = mysql_connect($this->_config["DBHOST"], $this->_config["DBUSER"], $this->_config["DBPASS"]);
            }
            if ($this->_dbh && mysql_errno() == 0) {
                $this->_isConnected = true;
            }
            $this->SelectDatabase($this->_config["DBNAME"]);
            return $this;
        } else {
            throw new ErrorException("Config values not properly defined");
        }
    }
    
    public function SelectDatabase($name) {
        //print "SCDB::SelectDatabase()\n";
        if ($this->_isConnected) {                
            mysql_select_db($name);
            if ($asPersistent) {$this->_isPersistent = true;}
        } else {
            $this->_config['DBNAME'] = $name;
        }
        
        return $this;
    }


    public function isConnected() {
        if ($this->_isPersistent) {
            $this->CheckPersistentConnection();
        }
        return $this->_isConnected;
    }
    
    public function setConfig($key, $value) {
        $this->_config[$key] = $value;
        return $this;
    }
    
    
    public function checkConnection() {
        // print "SCDB::CheckConnection\n";
        if (!$this->_isConnected) {
            $this->connect($this->_isPersistent);
        }
        return $this;
    }
    
    public function checkPersistentConnection() {
        if ($this->_isPersistent) {
            if (!mysql_ping($this->_dbh)) {
                $this->connect(true);
            }
        }
        return $this;
    }
    
    public function Q($query) {
        //print "SQDB::Q()\n";
        $res = null;
        $this->checkConnection();
        
        $res = mysql_query($query);
        $this->_lastError = mysql_error();
        if (!empty($this->_lastError)) {
            throw new ErrorException($this->_lastError);
        }
        return $res;
    }
    
    public function getInstance() {
        return $this->_dbh;
    }
    
    private function getSelectedDatabase(){
        $this->_selectedDatabase = $this->_dbh->GetSelectedDatabase();
        if ($this->_selectedDatabase == NULL) {
            throw new ErrorException("Database is not selected");
        }
        return $this;
    }
}
